import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'dva'
import { routerRedux } from 'dva/router'
import { Page } from 'components'
import queryString from 'query-string'
import Filter from './components/Filter'
import List from './components/List'
import { ChartCard } from 'components/Charts'


const PerformancesOfWxAccount = ({ location, dispatch, loading, performancesOfWxAccount }) => {
  const { query, pathname } = location
  const { list, pagination, orderType, adAcquireType, dimension } = performancesOfWxAccount
  const handleRefresh = (newQuery) => {
    dispatch(routerRedux.push({
      pathname,
      search: queryString.stringify({
        ...query,
        ...newQuery
      })
    }))
  }

  const filterProps = {
    filter: {
      ...query
    },
    adAcquireType,
    orderType,
    dimension,
    onFilterChange(value) {
      handleRefresh({
        ...value,
        current: 1
      })
    },
    handleOnChange(value) {
      dispatch({
        type: 'performancesOfWxAccount/updateDimension',
        payload: {
          dimension: value
        }
      })
    }
  }

  const listProps = {
    rowKey: 'id',
    dataSource: list,
    loading: loading.effects['performancesOfWxAccount/query'],
    pagination,
    location,
    onChange(page) {
      handleRefresh({
        current: page.current,
        size: page.pageSize
      })
    }
  }

  return (
    <div>
      <ChartCard style={{ marginBottom: 12 }}>
        <Filter {...filterProps} />
      </ChartCard>
      <Page inner>
        <List {...listProps} />
      </Page>
    </div>
  )
}

PerformancesOfWxAccount.propTypes = {
  performancesOfWxAccount: PropTypes.object,
  location: PropTypes.object,
  loading: PropTypes.object
}
export default connect(({ performancesOfWxAccount, loading }) => ({ performancesOfWxAccount, loading }))(PerformancesOfWxAccount)
